package com.yoomiito.app.adapter.goods.list.sort

import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.yoomiito.app.R
import com.yoomiito.app.model.gift.GiftGoods
import com.yoomiito.app.utils.ImageLoader
import com.yoomiito.app.utils.L
import com.yoomiito.app.utils.SpannableUtils
import com.yoomiito.app.widget.CouponView
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter
import com.zhy.view.flowlayout.TagFlowLayout
import java.util.ArrayList

class StarStoreGoodsAdapter(data:List<GiftGoods>?) : BaseQuickAdapter<GiftGoods, BaseViewHolder>(R.layout.item_gife_goods, data){

    override fun convert(helper: BaseViewHolder?, item: GiftGoods?) {
        helper?.getView<ImageView>(R.id.item_gift_pic).let {
            ImageLoader.getInstance().into(mContext, item?.product_img, it ,R.drawable.goods_default)
        }
        helper?.setGone(R.id.goodsFlag,false)
        helper?.setText(R.id.goodsTitle,item?.product_name)

        //价格
        helper?.setText(R.id.goodsPrice,"¥${SpannableUtils.handlePrice(item?.sale_price)}")
        helper?.setText(R.id.goodsSaleSize, "已售${SpannableUtils.saleNum(item?.volume.toString())}")
        if(!SpannableUtils.isEmptyOr0(item?.market_price)){
            helper?.setText(R.id.oldGoodsPrice,SpannableUtils.getSpannable("¥${SpannableUtils.handlePrice(item?.market_price)}",0))
        }
        //佣金
//        val awardView = helper?.getView<View>(R.id.item_gift)
        helper?.setGone(R.id.awardView,false)
        val couponView = helper?.getView<CouponView>(R.id.couponView)
        if (item?.yo_money!! > 0) {
            couponView?.visibility = View.VISIBLE
            couponView?.setText("${item.yo_money}优米")
            couponView?.setHeaderText("送")
        } else {
            couponView?.visibility = View.GONE
        }

        //标签
        val tagFlowLayout = helper?.getView<TagFlowLayout>(R.id.item_gift_flowLayout)
        val s = ArrayList<String>()
        if (!TextUtils.isEmpty(item?.product_point)) {
            s.add(item!!.product_point)
        }
        tagFlowLayout?.adapter = object : TagAdapter<String>(s) {
            override fun getView(parent: FlowLayout, position: Int, o: String): View {
                val tv = LayoutInflater.from(mContext).inflate(R.layout.item_goods_flag, parent, false) as TextView
                val lp = tv.layoutParams as ViewGroup.MarginLayoutParams
                lp.leftMargin = 0
                //tv.setLayoutParams(lp);
                tv.text = if (o.length > 8) o.substring(0, 8) + "..." else o
                return tv
            }
        }
    }

}